using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Administration;
using System.Runtime.InteropServices;
using System.Security.Permissions;

namespace Voodoo.SharePoint
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("18d9d5d5-a8e2-470c-963b-6d9977515ea5")]
    public class ToolkitFeatureEventReceiver : SPFeatureReceiver
    {
       
        //public override void FeatureActivated(SPFeatureReceiverProperties properties)
        //{
        //    if (!System.Diagnostics.Debugger.IsAttached)
        //    {
        //        System.Diagnostics.Debugger.Launch();
        //    }
        //    System.Diagnostics.Trace.WriteLine("FeatureActivated");
        //}


         
        //public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        //{
        //    if (!System.Diagnostics.Debugger.IsAttached)
        //    {
        //        System.Diagnostics.Debugger.Launch();
        //    }
        //    System.Diagnostics.Trace.WriteLine("FeatureDeactivating");
        //}

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //{
        //    if (!System.Diagnostics.Debugger.IsAttached)
        //    {
        //        System.Diagnostics.Debugger.Launch();
        //    }
        //    System.Diagnostics.Trace.WriteLine("FeatureInstalled");
        //}



        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            //first set our global settings to default values
            Voodoo.SharePoint.ReadUnreadMarks.ReadUnreadFarmSettings globalSettings = (Voodoo.SharePoint.ReadUnreadMarks.ReadUnreadFarmSettings)SPFarm.Local.GetObject(Voodoo.SharePoint.ReadUnreadMarks.ReadUnreadFarmSettings.SETTINGS_ID);
            if (null != globalSettings)
            {
                globalSettings.WriteDirect = false;
                globalSettings.TrackDocuments = false;
                globalSettings.DocumentTrackerBackgroundThreadCount = 0;
                globalSettings.Update(false);
            }

            //now remove any web.config entries we may have left behind
            SPWebService webService = SPWebService.ContentService;
            System.Collections.ObjectModel.Collection<SPWebConfigModification> modificationCollection = webService.WebConfigModifications;
            int modificationCount = modificationCollection.Count;
            //walk backwards after preserving count because we will modify the
            //collection during this loop
            for (int countIndex = modificationCount - 1; countIndex > -1; countIndex--)
            {
                if ((modificationCollection[countIndex].Owner == Voodoo.SharePoint.ReadUnreadMarks.UtilityMethods.WEB_CONFIG_OWNER))
                {
                    SPWebConfigModification foundModification = modificationCollection[countIndex];
                    modificationCollection.Remove(foundModification);
                    webService.Update();
                    webService.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
                }
            }
            webService.Update();
            webService.ApplyWebConfigModifications();

        }

   

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //    if (!System.Diagnostics.Debugger.IsAttached)
        //    {
        //        System.Diagnostics.Debugger.Launch();
        //    }
        //    System.Diagnostics.Trace.WriteLine("FeatureUpgrading");
        //}
    }
}
